package client.effects;

import java.util.Random;

import org.newdawn.slick.Image;
import org.newdawn.slick.particles.Particle;
import org.newdawn.slick.particles.ParticleEmitter;
import org.newdawn.slick.particles.ParticleSystem;
import org.newdawn.slick.util.FastTrig;

public class MuzzleEmitter implements ParticleEmitter {

	private float posX, posY;
	private float angle = 90;

	private int life = 1000;

	private Random rng = new Random();

	private int interval = 5;
	private int timer;

	public MuzzleEmitter(float x, float y) {
		this.posX = x;
		this.posY = y;
	}

	public MuzzleEmitter(float x, float y, int life) {
		this(x, y);
		this.life = life;
	}

	public void setPosition(float x, float y) {
		posX = x;
		posY = y;
	}

	@Override
	public void update(ParticleSystem system, int delta) {
		timer += delta;
		if (timer >= interval) {
			timer = 0;
			Particle p = system.getNewParticle(this, life);
			p.setPosition(posX, posY);
			p.setSize((rng.nextInt(10)) * 2f);
			float vx = (float) FastTrig.cos(Math.toRadians(angle));
			float vy = (float) FastTrig.sin(Math.toRadians(angle));
			p.setVelocity(vx, vy, 0f);
		}
	}

	@Override
	public void updateParticle(Particle particle, int delta) {
		float c = 0.005f * delta;
		particle.adjustColor(0, -c / 2, -c * 2, -c / 4);
	}

	@Override
	public boolean completed() {
		return false;
	}

	@Override
	public void wrapUp() {
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public void setEnabled(boolean enabled) {
	}

	@Override
	public boolean useAdditive() {
		return false;
	}

	@Override
	public Image getImage() {
		return null;
	}

	@Override
	public boolean isOriented() {
		return false;
	}

	@Override
	public boolean usePoints(ParticleSystem system) {
		return false;
	}

	@Override
	public void resetState() {
	}

}
